package org.sixtynine.stock.dto;

import java.util.ArrayList;
import java.util.Collection;

public class Page<T> {
	private static final long serialVersionUID = 3617869045664714881L;

	private long totalRecords = 0;

	private int startIndex = 0;

	private int noOfRecords = 0;

	private int pageLength = 0;

	/** max number of pages to be displayed in the page */
	public static final int noOfPages = 10;

	private Collection<T> page = new ArrayList<T>();

	public Page() {
	}

	public Page(long totalRecords, int startIndex, int noOfRecords,
			Collection<T> page) {
		this.totalRecords = totalRecords;
		this.startIndex = startIndex;
		this.noOfRecords = noOfRecords;
		this.page = page;
	}

	public Page(long totalRecords, int startIndex, int noOfRecords,
			int pageLength, Collection<T> page) {
		this.totalRecords = totalRecords;
		this.startIndex = startIndex;
		this.noOfRecords = noOfRecords;
		this.pageLength = pageLength;
		this.page = page;
	}

	public int getNoOfRecords() {
		return noOfRecords;
	}

	public Collection<T> getPageData() {
		return page;
	}

	public int getStartPosition() {
		return startIndex;
	}

	public long getTotalNoOfRecords() {
		return totalRecords;
	}

	public int getPageLength() {
		return pageLength;
	}

	public int getTotalNoOfPages() {
		int totalNoOfPages = 1;
		if (pageLength != 0) {
			totalNoOfPages = (int) totalRecords / pageLength;
			if (totalRecords % pageLength > 0)
				totalNoOfPages++;
		}
		return totalNoOfPages;
	}

	public int getCurrentPageNo() {
		int currentPageNo = 1;
		if (pageLength != 0) {
			currentPageNo = startIndex / pageLength + 1;
		}
		return currentPageNo;
	}

	public boolean isShowPrev() {
		return getCurrentPageNo() > 1;
	}

	public boolean isShowNext() {
		return getCurrentPageNo() < getTotalNoOfPages();
	}

	public int getStartPageNo() {
		int currentPageNo = getCurrentPageNo();
		if (currentPageNo <= noOfPages) {
			return 1;
		} else {
			return ((currentPageNo + 1) - noOfPages);
		}
	}

	public int getEndPageNo() {
		int totalNumOfPages = getTotalNoOfPages();
		int startPageNumber = getStartPageNo();

		if (totalNumOfPages - startPageNumber > noOfPages) {
			return (startPageNumber + noOfPages -1);
		} else {
			return totalNumOfPages;
		}
	}

	public void setPageData(Collection<T> pageData) {
		this.page = pageData;
	}

	public void setNoOfRecords(int endPosition) {
		this.noOfRecords = endPosition;
	}

	public void setTotalNoOfRecords(long totalNoOfRecords) {
		this.totalRecords = totalNoOfRecords;
	}

	public void setPageLength(int pageLength) {
		this.pageLength = pageLength;
	}

}
